//
//  DerbyStopwatch.m
//  DerbyOSSampler
//
//  Created by glenn andreas on 3/6/14.
//  Copyright (c) 2014 glenn andreas. All rights reserved.
//

#import "DerbyStopwatch.h"

@interface DerbyStopwatch()
@property (strong, nonatomic) NSTimer *timer;
@property (assign, nonatomic) NSTimeInterval startTime;
@property (assign, readwrite, nonatomic) NSTimeInterval ellapsedTime;
@end
@implementation DerbyStopwatch
+ (NSSet *) keyPathsForValuesAffectingTimeString
{
    return [NSSet setWithObjects:@"ellapsedTime",@"countsUp", @"maximumTime", nil];
}
+ (NSSet *) keyPathsForValuesAffectingIsRunning
{
    return [NSSet setWithObjects:@"timer", nil];
}
+ (NSSet *) keyPathsForValuesAffectingTimeLeft
{
    return [NSSet setWithObjects:@"ellapsedTime",@"maximumTime", nil];
}
- (id) initWithMaximumTime:(NSTimeInterval)maximumTime
{
    self = [super init];
    if (self) {
        _maximumTime = maximumTime;
    }
    return self;
}
- (id) initWithMaximumTime: (NSTimeInterval) maximumTime countsUp: (BOOL) countsUp;
{
    self = [super init];
    if (self) {
        _maximumTime = maximumTime;
        _countsUp = countsUp;
    }
    return self;
}
- (void) start
{
    [self start: 0];
}
- (void) start: (NSTimeInterval) time
{
    [self reset: 0];
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(updateEllapsedTime:) userInfo:nil repeats:YES];
    }
}
- (void) stop
{
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
}
- (void) resume
{
    if (self.isRunning)
        return;
    // need to tweak with time before running
    self.startTime = [NSDate timeIntervalSinceReferenceDate] - self.ellapsedTime;
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(updateEllapsedTime:) userInfo:nil repeats:YES];
    }
}
- (void) reset
{
    [self reset:0];
}
- (void) reset: (NSTimeInterval) time
{
    self.startTime = [NSDate timeIntervalSinceReferenceDate] - time;
    self.ellapsedTime = time;
}
- (void) updateEllapsedTime: (NSTimer *) timer
{
    NSTimeInterval curTime = [NSDate timeIntervalSinceReferenceDate] - self.startTime;
    if (curTime >= self.maximumTime) {
        curTime = self.maximumTime;
    }
    if (curTime == self.maximumTime) {
        [self stop];
    }
    self.ellapsedTime = curTime;
}
- (BOOL) isRunning
{
    return self.timer.isValid;
}
- (NSTimeInterval) timeLeft
{
    return self.maximumTime - self.ellapsedTime;
}
- (NSString *) timeString
{
    NSTimeInterval time;
    if (self.countsUp) {
        time = self.ellapsedTime;
    } else {
        time = self.maximumTime - self.ellapsedTime;
    }
    return [[self class] formatTime:time];
}
+ (NSString *) formatTime: (NSInteger) time; // [h:m]m:ss
{
    if (time > 60 * 60) {
        return [NSString stringWithFormat:@"%td:%.2td:%.2td", time / 3600, (time / 60) % 60, time % 60];
    } else {
        return [NSString stringWithFormat:@"%td:%.2td", (time / 60) % 60, time % 60];
    }
}
@end
